﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using JKF.Substructure;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;

namespace JKF.DataModule.SqlServerDAL
{
    public partial  class dalSYS_ROLE:IDAL.IdalSYS_ROLE
    {
        /// <summary>
        /// 根据角色role_key获取角色名称
        /// </summary>
        /// <param name="role_key"></param>
        /// <returns></returns>
        public static string GetRoleNameByKeyDAL(string role_key)
        {
            string sql = "select ROLE_NAME from dbo.SYS_ROLE  where ROLE_KEY='"+role_key+"'";
            //SqlParameter [] parameters = {
            //                             new SqlParameter("@ROLE_KEY", SqlDbType.UniqueIdentifier,16)
            //                             };
            //parameters[0].Value = new Guid(role_key); 
            object obj = DB.HR.ExecuteScalar(CommandType.Text, sql);
            return obj.ToString();
        } 

        // 定义要获取哪些字段ROLE_KEY, ROLE_NAME, ROLE_DESCRIPTON, DATE_CREATE, DATE_UPDATE
        private const string FieldShow = "ROLE_KEY, ROLE_NAME, ROLE_DESCRIPTON, DATE_CREATE, DATE_UPDATE";
        private const string FieldOrder = "ROLE_NAME";
        private const string FieldKey = "ROLE_KEY";
        /// <summary>
        /// 获取角色列表
        /// </summary>
        /// <param name="ROLE_NAME">角色名称</param>
        /// <param name="pageCurrent">当前页码</param>
        /// <param name="pageSize">一页显示多少</param>
        /// <param name="pageCount">总页数</param>
        /// <param name="rowCount">总记录数</param>
        /// <returns>返回DataTable数据</returns>
        public static DataTable GetSys_RolesDAL(string ROLE_NAME, int pageCurrent, int pageSize, ref int pageCount, ref int rowCount)
        {
            var sql = new StringBuilder(@"  1=1 ");
            // 下面组装查询条件
            if (!string.IsNullOrEmpty(ROLE_NAME))
            {
                sql.AppendFormat(" AND ROLE_NAME  LIKE '%{0}%'", ROLE_NAME);
            }
            // 下面调用此方法去查询数据库
            return DataPage.GetPageInfo("dbo.SYS_ROLE", FieldKey, pageCurrent, pageSize, FieldShow, FieldOrder, sql.ToString(), ref pageCount, ref rowCount).Tables[0];

        }
         
        /// <summary>
        /// 判断是否存在此名称,true表示存在
        /// </summary>
        /// <param name="shop_name">角色名称</param>
        /// <returns></returns>
        public static bool ExistsSysRoleNameDAL(string sys_role_name)
        {
            object obj = DB.HR.ExecuteScalar(CommandType.Text, "select count(1) from dbo.SYS_ROLE  where ROLE_NAME='" + sys_role_name + "'");
            return Convert.ToInt32(obj) > 0;
        }

         /// <summary>
       /// 采用事务提交生成的Insert语句
       /// </summary>
       /// <param name="sql"></param>
        public static void TransactionInsertRole_Module_ActionDAL(string sql)
        {
            //采用事务提交
            using (TransactionHR tHR = DB.BeginTransactionHR())
            {
                int result = tHR.ExecuteNonQuery(sql);
                if (result > 0)
                {
                    tHR.Commit();
                }
                else
                {
                    tHR.Rollback();
                }
            }
        }
        /// <summary>
        /// 采用事务提交Role生成的Update语句
        /// </summary>
        /// <param name="sql"></param>
        public static void TransactionUpdateRole_Module_ActionDAL(string sql)
        {
            //采用事务提交
            using (TransactionHR tHR = DB.BeginTransactionHR())
            {
                int result = tHR.ExecuteNonQuery(sql);
                if (result > 0)
                {
                    tHR.Commit();
                }
                else
                {
                    tHR.Rollback();
                }
            }
        }
        /// <summary>
        /// 采用事务提交Role生成的delete语句:此方法作废。改用存储过程
        /// 修改日期：2013.1.15
        /// </summary>
        /// <param name="sql"></param>
        public static void TransactionDeleteRole_Module_ActionDAL(string sql)
        {
            //采用事务提交
            using (TransactionHR tHR = DB.BeginTransactionHR())
            {
                int result = tHR.ExecuteNonQuery(sql);
                if (result > 0)
                {
                    tHR.Commit();
                }
                else
                {
                    tHR.Rollback();
                }
            }
        }
        /// <summary>
        /// 删除角色，调用事务存储过程
        /// 日期：2013.1.15
        /// </summary>
        /// <param name="role_key"></param>
        /// <returns></returns>
        public static string StoreProcedureDeleteRole_Module_ActionDAL(string role_key)
        {
            int result = -1;
            Database db = DB.HR;
            DbCommand dbCommand = db.GetStoredProcCommand("[dbo].[proc_RoleDelete]");
            db.AddInParameter(dbCommand, "@ROLE_KEY", DbType.Guid, new Guid(role_key));
            db.AddOutParameter(dbCommand, "@result", DbType.Int32, result);
            db.ExecuteNonQuery(dbCommand);
            return dbCommand.Parameters["@result"].Value.ToString();
        }
    }
}
